package com.izhengyin.miaosha.controller;
import com.izhengyin.miaosha.service.MiaoShaService;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.info.Info;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.Map;

@RestController
@RequestMapping("/miaosha")
@OpenAPIDefinition( info = @Info(title = "秒杀服务接口接口"))
public class MiaoShaController {
    private final MiaoShaService miaoShaService;
    public MiaoShaController(MiaoShaService miaoShaService) {
        this.miaoShaService = miaoShaService;
    }

    @Operation(summary = "请求排队，获取排队号")
    @PostMapping("/{itemId}/enqueue")
    public Mono<String> enqueue(@PathVariable long itemId){
        return miaoShaService.enqueue(itemId);
    }

    @Operation(summary = "通过排队号，获取token")
    @GetMapping("/{itemId}/token/{queueNumber}")
    public Mono<String> getTokenByQueueNumber(@PathVariable long itemId,@PathVariable String queueNumber){
        return miaoShaService.getToken(queueNumber);
    }

    @Operation(summary = "创建订单")
    @PostMapping("/{itemId}/order")
    public Mono<Boolean> createOrder(@PathVariable long itemId , @RequestBody Map<String,Object> args){
        return miaoShaService.createOrder(itemId,args)
                .defaultIfEmpty(false);
    }
}
